package com.hotel.reservation.controller.admin;

import com.hotel.reservation.po.RoomPo;
import com.hotel.reservation.service.ReserveService;
import com.hotel.reservation.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 0.1
 * @Author: oliver chen
 * @Description: 后台客房服务
 * @Date:Create：in 2021/7/27 11:39
 * @Modified By：
 */
@Controller
@RequestMapping("/admin/room")
public class AdminRoomController {
    private String[] roomType = {"单人房","智能亲子房","豪华双床房","阳光大床房","甄选榻榻米","行政房"};
    private String[] roomState = {"未预定","已预订","已入住"};

    @Autowired
    private RoomService roomService;
    @Autowired
    private ReserveService reserveService;

    @RequestMapping("/selectAllRooms")
    public String selectAllRooms(Model model,Integer pageCur,String flag){
        List<RoomPo> allRooms = roomService.selectAll();
        if (allRooms==null){
            allRooms=new ArrayList<>();
        }
        int temp =allRooms.size();
        model.addAttribute("totalCount",temp);
        int totalPage=0;
        if (temp==0){
            totalPage=0;//总页数
        }else {
            //返回大于或等于指定表达式的最小整数
            totalPage=(int)Math.ceil((double) temp/10);
        }
        if (pageCur==null){
            pageCur=1;
        }
        if ((pageCur-1)*10>temp){
            pageCur=pageCur-1;
        }
        //分页查询
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("startIndex",(pageCur-1)*10);//起始位置
        map.put("perPageSize",10);//每页10个
        allRooms = roomService.selectAllRoomByPage(map);
        model.addAttribute("allRooms",allRooms);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("pageCur",pageCur);
        model.addAttribute("roomType",roomType);
        model.addAttribute("roomState",roomState);
        model.addAttribute("flag",flag);
        if (flag.equals("select")||flag.equals("update")){
        return "admin/selectRooms";
        }else {
            return "admin/deleteSelectRooms";
        }
    }
    @RequestMapping("/selectARoom")
    public String selectARoom(Model model,String id){
        RoomPo roomPo = roomService.selectByRoomId(id);
        if (roomPo!=null){
            model.addAttribute("roomDetail",roomPo);
            model.addAttribute("roomType",roomType);
            model.addAttribute("roomState",roomState);
        }
        return "admin/roomsDetail";
    }
    @RequestMapping("/toInsertARoom")
    public String toInsertARoom(Model model){
        model.addAttribute("rooms",new RoomPo());
        HashMap<Integer,String> map = new HashMap<>();
        for (int i=0;i<roomType.length;++i){
            map.put(i,roomType[i]);
        }
        model.addAttribute("roomTypes",map);
        HashMap<Integer,String> map1 = new HashMap<>();
        for (int o=0;o<roomState.length;++o){
            map1.put(o,roomState[o]);
        }
        model.addAttribute("roomStates",map1);
        return "admin/addRooms";
    }
    @RequestMapping("/insertARoom")
    public String insertARoom(Model model,RoomPo roomPo){
        if (roomService.selectByRoomId(roomPo.getRoomId())!=null){
            System.out.println("Error! Room Id has been already exist");
            model.addAttribute("error",0);
            return "error";
        }else {
        roomService.insertARoom(roomPo);
        System.out.println("Successfully Insert A Room Information!!!");
        model.addAttribute("message","添加成功");
        return "forward:/admin/room/selectAllRooms?flag=select";
        }
    }
    @RequestMapping("/toUpdateARoom")
    public String toUpdateARoom(String roomId,Model model){
        RoomPo roomPo = roomService.selectByRoomId(roomId);
        model.addAttribute("room",roomPo);
        HashMap<Integer,String> roomTypes = new HashMap<>();
        for (int i=0;i<roomType.length;++i){
            roomTypes.put(i,roomType[i]);
        }
        model.addAttribute("roomTypes",roomTypes);
        HashMap<Integer,String>  roomStates= new HashMap<>();
        for (int i=0;i<roomState.length;++i){
            roomStates.put(i,roomState[i]);
        }
        model.addAttribute("roomStates",roomStates);
        return "/admin/updateARooms";
    }
    @RequestMapping("/updateARoom")
    public String updateARoom(RoomPo room,Model model){
        int flag  = roomService.alterARoom(room);
        if (flag>0){
            model.addAttribute("message","修改成功");
            return "forward:/admin/room/selectAllRooms?flag=select";
        }else {
            return "error";
        }
    }
    //删除一个
    @RequestMapping("/deleteARoom")
    public String deleteARoom(String roomId,Model model){
        if (reserveService.selectByRoomId(roomId).size()>0){
            model.addAttribute("message","有订单包含该房间号，不允许删除");
            return "forward:/admin/room/selectAllRooms?flag=delete";
        }else {
            roomService.deleteARoom(roomId);
            model.addAttribute("message","删除成功！");
            return "forward:/admin/room/selectAllRooms?flag=delete";
        }
    }
    //删除多个
    @RequestMapping("/deleteRooms")
    public String deleteRooms(String[] ids,Model model){
        List<String> list  =new ArrayList<String>();
        for (String i:ids){
            if (reserveService.selectByRoomId(i).size()>0){
                model.addAttribute("message","有订单包含该房间号，不允许删除");
                return "forward:/admin/room/selectAllRooms?flag=delete";
            }else {
                list.add(i);
            }
        }
        roomService.deleteRooms(list);
        model.addAttribute("message","删除成功！");
        return "forward:/admin/room/selectAllRooms?flag=delete";
    }
}
